package com.bobi.tfca.ui.mainnavigation.home.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.activity.addCallback
import androidx.lifecycle.ViewModel
import androidx.navigation.findNavController
import com.bobi.tfca.R
import com.bobi.tfca.base.BaseFragment
import com.bobi.tfca.databinding.FragmentOrgActConditionBinding
import com.bobi.tfca.databinding.FragmentOrganizeActivityBinding
import com.bobi.tfca.util.LogUtil

class OrgActConditionFragment : BaseFragment<FragmentOrgActConditionBinding, ViewModel>(
    FragmentOrgActConditionBinding::inflate,
    null,
    true
) {

    @SuppressLint("ResourceType", "UseRequireInsteadOfGet")
    override fun initFragment(
        binding: FragmentOrgActConditionBinding,
        viewModel: ViewModel?,
        savedInstanceState: Bundle?
    ) {
        // 设置顶部样式
        setTopPanelStyle(binding)

        // 初始化条件
        binding.signCondition.setText(publicViewModel!!.organizeActivity.signCondition)

        // 在返回上一个页面的时候，将文字保存起来，最好是根据事件来算，而不是简单的上一步按钮，因为用户可能按系统功能键
        // 捕获返回键事件
        requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner) {
            LogUtil.i(binding.signCondition.text.toString())
            publicViewModel!!.organizeActivity.signCondition = binding.signCondition.text.toString()
            requireActivity().findNavController(R.id.app_nav).navigateUp()
        }

        binding.previousStep.setOnClickListener {
            publicViewModel!!.organizeActivity.signCondition = binding.signCondition.text.toString()
            requireActivity().findNavController(R.id.app_nav).navigateUp()
        }

        // 设置点击下一步按钮
        binding.nextStep.setOnClickListener {
            publicViewModel!!.organizeActivity.signCondition = binding.signCondition.text.toString()
            requireActivity().findNavController(R.id.app_nav).navigate(R.id.action_orgActConditionFragment_to_orgActDetailsFragment)
        }
    }


    /**
     * 设置顶部面板的样式
     */
    private fun setTopPanelStyle(binding: FragmentOrgActConditionBinding) {
        val topPanel = binding.topPanel
        topPanel.leftImage.setOnClickListener {
            publicViewModel!!.organizeActivity.signCondition = binding.signCondition.text.toString()
            requireActivity().findNavController(R.id.app_nav).navigateUp()
        }
        val rightLayout = topPanel.rightLayout
        rightLayout.visibility = View.GONE
        topPanel.titleText.text = "报名条件"
    }
}